﻿using Newtonsoft.Json;
using River.Core;
using River.IntAI.ISdk;
using System;
using System.IO;
using System.Linq;
using System.Text;

namespace River.IntAI
{
    public partial class FrmOcr : BaseForm
    {
        bool showFileInfo = false;/// 显示文件信息标志
        StringBuilder sb = new StringBuilder();
        public FrmOcr()
        {
            InitializeComponent();
            showFileInfo = checkShowDetail.Checked;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            if (River.Core.Utils.selectSingleFile(ref fileName, "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp"))
            {
                tbFileName.Text = fileName;
                // 识别
                if (!checkFile()) return;
                ShowStatus(string.Format("文件[{0}]识别结果：", fileName), true);
                OcrUtils ocrUtils = new OcrUtils();
                BaiduRetOcr jObject = ocrUtils.Recognize(tbFileName.Text, chkAccurate.Checked);
                ShowStatus(JsonConvert.SerializeObject(jObject), true);
                if (jObject != null && jObject.words_result != null)
                {
                    sb.Clear();
                    jObject.words_result.ToList().ForEach(x => sb.AppendLine(x.words));
                    ShowStatus(sb.ToString(), false, true);
                }
                else
                {
                    ShowStatus("无识别结果：" + jObject.error_msg);
                }
            }
        }

        /// <summary>
        /// 显示状态信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="skiptime"></param>
        private void ShowStatus(string msg, bool checkShow = false, bool skiptime = false)
        {
            if (checkShow && !showFileInfo) return;
            this.InvokeDele(new Action(() =>
            {
                rtbStatus.AppendText((skiptime ? "" : River.Core.Utils.NowTime() + ": ") + msg + "\r\n");
                rtbStatus.Select(rtbStatus.Text.Length, 0);
                rtbStatus.ScrollToCaret();
            }));
        }
        /// <summary>
        /// 检测用户选择的文件
        /// </summary>
        /// <returns></returns>
        private bool checkFile()
        {
            string fileName = tbFileName.Text;

            if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName))
            {
                MsgUtil.Error("请选择有效的图片文件");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 清空状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEmptyStatus_Click(object sender, EventArgs e)
        {
            this.InvokeDele(new Action(() =>
            {
                rtbStatus.Text = string.Empty;
            }));
        }
        /// <summary>
        /// 是否显示详情
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkShowDetail_CheckedChanged(object sender, EventArgs e)
        {
            showFileInfo = checkShowDetail.Checked;
        }
        /// <summary>
        /// 批量识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBatch_Click(object sender, EventArgs e)
        {
            string[] selectedFiles;
            if (!Core.Utils.selectMulFiles(out selectedFiles, "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp")) return;
            OcrUtils ocrUtils = new OcrUtils();
            ShowStatus("结果如下：");
            foreach (var item in selectedFiles)
            {
                ShowStatus(string.Format("文件[{0}]识别结果：", item), true);
                BaiduRetOcr jObject = ocrUtils.Recognize(item, chkAccurate.Checked);
                ShowStatus(JsonConvert.SerializeObject(jObject), true);
                if (jObject != null && jObject.words_result != null)
                {
                    sb.Clear();
                    jObject.words_result.ToList().ForEach(x => sb.AppendLine(x.words));
                    ShowStatus(sb.ToString(), false, true);
                }
                else
                {
                    ShowStatus("无识别结果" + jObject.error_msg);
                }
            }
        }
    }
}
